/**   
 * @Title: Helper.java 
 * CopyRight(c)2010-DMX 
 */
package com.dmx.fw.web.util;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.dmx.fw.bean.UIComponentBean;
import com.dmx.fw.bean.QueryInfoBean;
import com.dmx.fw.bean.mapping.pojo.Log;
import com.dmx.fw.bean.mapping.pojo.Operator;
import com.dmx.fw.component.commonquery.QueryScriptDef;
import com.dmx.fw.component.util.LoaderContext;
import com.dmx.fw.constant.SystemConstant;
import com.dmx.fw.constant.ViewConstant;
import com.dmx.fw.service.interfaces.DictionaryService;
import com.dmx.fw.service.interfaces.QueryService;
import com.dmx.fw.service.interfaces.SystemLogService;
import com.dmx.fw.util.DateTimeUtils;
import com.dmx.fw.util.StrUtils;

import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;

/** 
 * @Description: 前台页面调用后台某些通用功能的封装类
 * @author: system
 * @date: 2006
 * @version V1.0 
 * 
 */
public class Helper  {
	
	private final static Logger logger =Logger.getLogger(Helper.class);
	
	
	/**
	 * @Description 清除session中有关页面提示信息
	 */
	public static void clearExecuteMsg(){
		Map session = ServletActionContext.getContext().getSession();
		if (session!=null){
			session.remove(ViewConstant.GLOBAL_ERROR_INFO_KEY);
			session.remove(ViewConstant.GLOBAL_SUCCESS_INFO_KEY);
			session.remove(ViewConstant.GLOBAL_DETAIL_ERROR_INFO_KEY);	
		}
	}
	
	/**
	 * @Description 判断是否有权限
	 * @param authUrl
	 * @return boolean
	 */
	public static boolean authFuncCheck(String authUrl){
		Map session = ServletActionContext.getContext().getSession();
		if (session!=null){
			Map authFuncMap = (Map)session.get(ViewConstant.USER_AUTH_FUNC_SESSION);
			if (authFuncMap.get(authUrl)!=null){
				return true;
			}
		}
		return false;
	}
	
	/**
	 * @Description 获得字典表或缓存中的列表
	 * @param codeName
	 * @param params
	 * @return List
	 */
    public static List getCodeList(String codeName,List params){
    	List list = null;
	    try{
	    	/*try{
	    		ActionContext context = ServletActionContext.getContext();
	    		if (context!=null){
			    	Map session = context.getSession();
			    	if (session!=null){
						Operator optr = (Operator) session.get(ViewConstant.OPERATOR_SESSION_KEY);
						if (optr!=null){
							mgrDmnCode = optr.getMgrDmnCode();
						}
			    	}
	    		}
	    	}catch(Exception e){
	    		
	    	}*/
	    	LoaderContext context=LoaderContext.getLoaderContext();
	    	DictionaryService dictionaryService = (DictionaryService)context.getBean("dictionaryService");
	        //list = dictionaryService.getDataListByCacheCode(codeName,params,mgrDmnCode);
	    	list = dictionaryService.getDataListByCacheCode(codeName,params);
	
	     }catch(Exception e){
	     }
         if (list==null)
        	list = new ArrayList();
         return list;
    }
    
    public static List getCodeList(String codeName){

    	return getCodeList(codeName,null);
    }
    
    /**
	 * @Description 获得字典表或缓存中的列表，封装成UIComponentBean，用于扩展标签
	 * @param codeName
	 * @param params
	 * @return List<UIComponentBean>
	 */
    public static List<UIComponentBean> getCodeListToUIComponentBean(String codeName,List params){
    	List list = getCodeList(codeName,params);
    	List<UIComponentBean> retList = new ArrayList<UIComponentBean>();
    	for(java.util.Iterator it= list.iterator();it.hasNext();){
    		List tmp = (List)it.next();
    		UIComponentBean bean = new UIComponentBean();
    		if(tmp.size()>0){
    			bean.setKey((String)tmp.get(0));
    			if(tmp.size()>1){
    				bean.setValue((String)tmp.get(1));
    			}
    		}
    		retList.add(bean);
    	}
    	return retList;
    }
    
    public static List<UIComponentBean> getCodeListToUIComponentBean(String codeName){
    	return getCodeListToUIComponentBean(codeName,null);
    }
    
    
    
    /**
     * @param key 验证是否为唯一性的关键字
     * @param isEdit 判定是否是更新的
     * @param CACH_CODE表中的codeName 缓存中的关键字
     * @return false表示是有重复的，true表示唯一性的
     */
    public static boolean isUniqueCheck(String key,String codeName,boolean isEdit){
    	if(StringUtils.isBlank(key) || StringUtils.isBlank(codeName)) {
    		System.out.println("没有传入关键字或缓存中的关键字");
    		return false;
    	}
    	List params=new ArrayList();
    	params.add(key);
    	List list=getCodeList(codeName,params);
    	
    	if(isEdit)
    	  return Integer.parseInt(((List)list.iterator().next()).iterator().next().toString())==1;
    	else
    	  return Integer.parseInt(((List)list.iterator().next()).iterator().next().toString())==0;
    }
    
    /**
	 * @Description 解析日期时间字符串
	 * @param dateTimeVal
	 * @param part: 要解析的部分year/month/day/hour/minute/second
	 * @return String
	 */
    public static String parseValue(String dateTimeVal,String part){
		Calendar calendar = Calendar.getInstance();
		
		if (dateTimeVal == null)
			dateTimeVal = "";
			
		if (!"".equals(dateTimeVal)){
			java.util.Date date = null;
		
			if (dateTimeVal.length()>10)
				date = DateTimeUtils.StringToDate(dateTimeVal,SystemConstant.CURR_JAVA_DATETIME_FORMATTER);
			else
				date = DateTimeUtils.StringToDate(dateTimeVal,SystemConstant.CURR_JAVA_DATE_FORMATTER);
			
			if (date==null)
				return "";
			
			calendar.setTime(date);
			
			if (part.equalsIgnoreCase("year")){
			    return Integer.toString(calendar.get(Calendar.YEAR));
			}else if (part.equalsIgnoreCase("month")){
				return StrUtils.formatIntToStr((calendar.get(Calendar.MONTH)+1),2);
			}else if (part.equalsIgnoreCase("day")){
				return StrUtils.formatIntToStr((calendar.get(Calendar.DATE)),2);
			}else if (part.equalsIgnoreCase("hour")){
				return StrUtils.formatIntToStr(calendar.get(Calendar.HOUR_OF_DAY),2);
			}else if (part.equalsIgnoreCase("minute")){
				return StrUtils.formatIntToStr(calendar.get(Calendar.MINUTE),2);
			}else if (part.equalsIgnoreCase("second")){
				return StrUtils.formatIntToStr(calendar.get(Calendar.SECOND),2);
			}
		}
		return "";		
	}
    
    /**
	 * @Description 获得当前系统时间
	 * @return String
	 */
    public static String getCurrStrTime(){
    	return DateTimeUtils.getNowStrTime();
    	
    }
    
    /**
     * 创建LOG
     * @param log
     */
    public static void createLog(Log log){
    	LoaderContext context=LoaderContext.getLoaderContext();
    	SystemLogService systemLogService = (SystemLogService)context.getBean("systemLogService");
    	systemLogService.createLog(log);
    }
    
    /**
	 * @Description 进行编码转换
	 * @param str
	 * @return String
	 */
    public static String decode(String str){
    	return StrUtils.decode(str);
    }
    
    /**
	 * @Description escapeHTML
	 * @param sourcestr
	 * @return String
	 */
    public static String escapeHTMLTags(String sourcestr){
    	return StrUtils.escapeHTMLTags(sourcestr);
    }
    
    /**
	 * @Description escapeHTML，对于超过长度超过120的只取50后用"..."代替
	 * @param sourcestr
	 * @return String
	 */
    public static String escapeHTMLShortTags(String sourcestr){
    	int sourcestrSize=sourcestr.length();
    	return sourcestrSize<120?StrUtils.escapeHTMLTags(sourcestr):StrUtils.escapeHTMLTags(sourcestr.substring(0,50)+"...");
    }
    
    /**
	 * @Description 根据busiId获得QueryInfoBean
	 * @param busiId
	 * @return QueryInfoBean
	 */
    public static QueryInfoBean getQueryConfigByBusiId(String busiId){
    	return (QueryInfoBean)QueryScriptDef.getQueryInfo(busiId);    
    }
    
    
}